package cn.wolfcode.crm.util;


import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice //@ControllerAdvice:Controller增强器
public class UnauthorizedExceptionUtil {
    //异常处理器
    @ExceptionHandler(UnauthorizedException.class)
    public void handler(HttpServletResponse response, HandlerMethod
            method, UnauthorizedException e) throws IOException {
        //如果贴了ResponseBody注解
        if (method.getMethod().isAnnotationPresent(ResponseBody.class)) {
            response.setContentType("text/json;charset=UTF-8");
            JsonResult result = new JsonResult();
            result.mark("对不起，您没有权限执行该操作");
            response.getWriter().print(JSON.toJSONString(result));
        } else {
            throw e;
        }
    }
}
